/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Conexao.java
 *
 * Created on 12/06/2009, 13:55:57
 */

package talkunix;

import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public class Conexao extends javax.swing.JFrame {

    /** Creates new form Conexao */
    public Conexao() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        IPConexao = new java.awt.Label();
        MenuConexao = new java.awt.Label();
        PortaConexao = new java.awt.Label();
        PortaCampo = new java.awt.TextField();
        CampoIP = new java.awt.TextField();
        ConectarButao = new java.awt.Button();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        IPConexao.setText("IP:");

        MenuConexao.setText("Menu de Conexão");

        PortaConexao.setText("Porta");

        PortaCampo.setText("");
        PortaCampo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                PortaCampoKeyPressed(evt);
            }
        });

        CampoIP.setText("");
        CampoIP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                CampoIPKeyPressed(evt);
            }
        });

        ConectarButao.setLabel("Conectar");
        ConectarButao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ConectarButaoMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(IPConexao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MenuConexao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PortaConexao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(PortaCampo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(CampoIP, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addComponent(ConectarButao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 141, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(ConectarButao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(CampoIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(MenuConexao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(19, 19, 19)
                            .addComponent(IPConexao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PortaConexao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PortaCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ConectarButaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ConectarButaoMouseClicked
        Iniciar();

    }//GEN-LAST:event_ConectarButaoMouseClicked

    private void CampoIPKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CampoIPKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                     Iniciar();
                 }
    }//GEN-LAST:event_CampoIPKeyPressed

    private void PortaCampoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PortaCampoKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                     Iniciar();
                 }
    }//GEN-LAST:event_PortaCampoKeyPressed

    public  void Iniciar(){

        String ip = this.CampoIP.getText();
        String porta = this.PortaCampo.getText();
        // int numero = Integer.parseInt(porta);
        ClienteServidor.Configurar(ip,9999);
        try {
            ClienteServidor.Conectar();
        } catch (UnknownHostException ex) {
            ClienteServidor.deuErro("Host Desconhecido!");
        } catch (IOException ex) {
            ClienteServidor.deuErro("Deu Erro!");
        }
        dispose();

    }

    /**
    * @param args the command line arguments
    */

    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            private Conexao janela;
            public void run() {
                janela = new Conexao();
                janela.setVisible(true);
            }
        });
    }
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField CampoIP;
    private java.awt.Button ConectarButao;
    private java.awt.Label IPConexao;
    private java.awt.Label MenuConexao;
    private java.awt.TextField PortaCampo;
    private java.awt.Label PortaConexao;
    // End of variables declaration//GEN-END:variables

}
